Ext.define('Emp.view.resource.OperatorSelect', {
	extend: 'Ext.window.Window',
	alias: 'widget.operatorselect',
	
	requires: ['Ext.ux.form.ItemSelector'],
	
	title: '分配操作',
	resizable: false,
	modal: true,
	width: 400,
	height: 300,
	layout: 'fit',
	
	initComponent: function() {
		var me = this;
		
		Ext.define('DataModel', {
			extend: 'Ext.data.Model',
			idProperty: 'id',
			fields: [
				{name: 'id', type: 'int'},
				{name: 'name', type: 'string'},
				{name: 'text', type: 'string'},
				{name: 'iconCls', type: 'string'},
				{name: 'status', type: 'int'},
				{name: 'sort', type: 'int'},
				{name: 'resourceId', type: 'int', useNull: true}
			]
		});
		
		var store = Ext.create('Ext.data.Store', {
			model: 'DataModel',
			autoLoad: true,	
			proxy: {
				type: 'ajax',
				url: PATH + '/security/findOperators/' + me.resource_id,
				reader: {
					type: 'json',
					root: 'datas',
					totalProperty: 'totalCount'
				}
			},
			listeners: {
				scope: this,
				'beforeload': function() {
        			this.setLoading(true);
        		},
				'load': function(_store, _records, _success) {
					var ids = [];
					for(var i = 0; i < _records.length; i++) {
						if(_records[i].get('resourceId') !== null) {
							ids.push(_records[i].get(_records[i].idProperty));
						}
					}
        			this.down('itemselector').setValue(ids);
        			this.setLoading(false);
        		}
			}
		});
		
		me.items = [{
			xtype: 'form',
			border: false,
			bodyPadding: 5,
			layout: 'anchor',
			defaults: {
				labelAlign: 'right',
				labelWidth: 60,
				anchor: '100%'
			},
			items: [{
					xtype: 'fieldcontainer',
					layout: 'hbox',
					items: [{
							xtype: 'label',
							text: '未分配选项',
							flex: 1
						}, {
							xtype: 'label',
							text: '已分配选项',
							flex: 1
					}]
				}, {
					xtype: 'itemselector',
		            store: store,
		            displayField: 'text',
		            valueField: 'id'
			}]
		}];
		
		me.buttons = [{
				xtype: 'splitbutton',
				text: '保&nbsp;&nbsp;存',
				iconCls: 'save',
				action: 'save',
				handler: function() {
					me.doSave(false);
				},
				menu: {
			        items: [{
		        		text: '保&nbsp;存&nbsp;并&nbsp;关&nbsp;闭',
						iconCls: 'close',
						action: 'save_close',
						handler: function() {
							me.doSave(true);
						}
		        	}]
			    }
			}, {
				text: '关&nbsp;&nbsp;闭',
				iconCls: 'close',
				action: 'close',
				handler: function() {
					me.close();
				}
		}];
		me.callParent();
	},
	
	doSave: function(close) {
		var me = this;
		var ids = me.down('itemselector').getValue();
		var opts = {
			method: 'post',
			url: PATH + '/security/saveOperators',
			params: {
				id: me.resource_id,
				ids: ids
			},
			success: function(response, options) {
				var result = Ext.decode(response.responseText);
				if (result.success) {
					Ext.show.msg('提示', '保存成功!');
					if(close) {
						me.close();
					}
				} else {
					Ext.Msg.show({
						title: '提示',
						msg: '保存失败!<br/>' + result.message,
						buttons: Ext.Msg.OK,
						icon: Ext.Msg.ERROR
					});
				}
			},
			failure: function(response, options) {
				Emp.msg.ajaxFailure(response, options)
			}
		};
		Ext.Ajax.request(opts);
	}
});